package com.lzwork.gpt.utility.caller;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.lzwork.web.RemoteCaller;

/**
 * @author LiZheng
 * @date 2023年12月6日 下午2:00:07
 */

public abstract class GptCaller extends RemoteCaller
{
	public static enum Caller
	{
		User,
		Assistant,
		System
	}

	public static enum CallType
	{
		Parser(0),
		OpenApi(1),
		Unknown(-1);

		int bit;

		CallType(int bit)
		{
			this.bit = bit;
		}

		public JSONObject toJObj()
		{
			JSONObject obj = new JSONObject();

			obj.put("id", bit);
			obj.put("title", name());

			return obj;
		}

		public static List<JSONObject> allInfo()
		{
			List<JSONObject> result = new ArrayList<>();

			for (CallType item : CallType.values())
			{
				if (item == Unknown)
					continue;

				result.add(item.toJObj());
			}

			return result;
		}

		public static CallType parseType(int bit)
		{
			for (CallType item : CallType.values())
			{
				if (item.bit == bit)
					return item;
			}
			return Unknown;
		}
	}

	public GptCaller(String serverUrl)
	{
		url = serverUrl;
		if (!url.endsWith("/"))
			url += "/";
	}

	protected String url;

	public String url()
	{
		return url;
	}

	@Override
	protected CallResult createResult()
	{
		return new GptCallResult();
	}

	public GptCallResult call(JSONObject param)
	{
		GptCallResult result = (GptCallResult) super.post(url, param, null);
		return result;
	}

	public GptCallResult call(String otherUrl, JSONObject param)
	{
		GptCallResult result = (GptCallResult) super.post(otherUrl, param, null);
		return result;
	}

	public JSONObject makeMessage(Caller caller, String content)
	{
		JSONObject obj = new JSONObject(true);

		obj.put("role", caller.name().toLowerCase());
		obj.put("content", content);

		return obj;
	}

	public abstract GptCallResult callChat(String chat, List<String> history, List<String> systemPrompt);
}
